
//
//  MYMyCollectGoodsListVC.m
//  MuYing
//
//  Created by tbb on 2019/8/8.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMyCollectGoodsListVC.h"
#import "MYShopSearchGoodsCell.h"

#import "YQPGoodsListModel.h"
#import "MYShopDetailsVC.h"
@interface MYMyCollectGoodsListVC ()

@property (strong, nonatomic) YQPGoodsListModel *model;

@property (strong, nonatomic) NSMutableArray *tieArray;

@end

@implementation MYMyCollectGoodsListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;

    self.tieArray = NSMutableArray.new;
    
    [self getUserGoodsCollectionsApi:YES];
    
    [self setupRefresh];
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserGoodsCollectionsApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.tieArray.count < 10) {
            [self.listTableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        self.page ++;

        [self getUserGoodsCollectionsApi:NO];
    }];
    // 自动改变透明度
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;
    [self.listTableView.mj_header beginRefreshing];
}
#pragma amrk - POST /api/collect/querySkuList查询商品收藏列表
- (void)getUserGoodsCollectionsApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;
    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);
    [self.tieArray removeAllObjects];
    [YQPNetWorkingManager sendPostRequest:kPOST_QUERY_GOODSCOLLECTION_LIST_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self stopRefresh];
        if (responseObject.code == 0) {
            self.model = [YQPGoodsListModel mj_objectWithKeyValues:responseObject.data];
            [self.tieArray addObjectsFromArray:self.model.list];
            [self.listTableView reloadData];
        }else {
            YQUITIPSERROR(responseObject.msg);
        }

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error);
        [self stopRefresh];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}

-(void)setupView {
    [super setupView];
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopSearchGoodsCell")
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tieArray.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPGoodsModel *model = nil;
    if (indexPath.row < self.tieArray.count) {
        model = self.tieArray[indexPath.row];
    }
    MYShopSearchGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopSearchGoodsCell"];

    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 112;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPGoodsModel *model = nil;
    if (indexPath.row < self.tieArray.count) {
        model = self.tieArray[indexPath.row];
    }
    MYShopDetailsVC *dVC  = [MYShopDetailsVC new];
    dVC.skuId = model.skuId;
    kYQPUSH(dVC);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

@end
